home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 901 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.2 KB  |  61 lines

  1. Path: engnews1.Eng.Sun.COM!taumet!clamage
  2. From: boukanov@sentef1.fi.uib.no (Igor Boukanov)
  3. Newsgroups: comp.std.c++
  4. Subject: Function object operator() is too anonymous.
  5. Date: 28 Mar 1996 20:27:18 GMT
  6. Organization: Fysisk institutt, Universitetet i Bergen
  7. Approved: clamage@eng.sun.com (comp.std.c++)
  8. Message-ID: <4jerpo$bvb@ugress.uib.no>
  9. NNTP-Posting-Host: taumet.eng.sun.com
  10. Keywords: C++, function object
  11. X-Nntp-Posting-Host: sentef1.fi.uib.no
  12. X-Newsreader: TIN [version 1.2 PL2]
  13. Content-Length: 1331
  14. X-Lines: 37
  15. Originator: clamage@taumet
  16.  
  17.    I noticed that that it impossible to write Function object class 
  18. (DWP 20.3) that can be used for example as Arithmetic operation object 
  19. and Predicate object in the same time, because both require operator() 
  20. that can not be overloaded! So why do not have instead of one anonymous 
  21. operator() a set of function obect methods like 
  22. unary_operation, binary_operation, predicate, comparison 
  23. and each function from algorithm.h will call correspondent method. 
  24. For example, find_if will call predicate method of function object and 
  25. transform will call unary_operation or binary_operation 
  26. instead of operator().
  27.  
  28. In this case one can write:
  29.  
  30. class F {
  31.    ...
  32.    bool predicate(int); // instead of bool operator()(int);
  33.    int unary_operation(int); // instead of int operator()(int);
  34.    int binary_operation(int, int); // instead of int operator()(int, int);
  35.    bool comparison(int, int); // instead of bool operator()(int, int);
  36. };
  37.  
  38. and somewhere use F:
  39.  
  40. vector<int> v1, v2;
  41. F f;
  42.  
  43. ...
  44. std::find_if(v1.begin(), v1.end(), f); 
  45.  // f.predicate(int) will  be called
  46. std::transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), f);
  47.  // f.binary_operation(int, int) will be called
  48. std::equal(v1.begin(), v1.end(), v2.begin(), f);
  49.  // f.comparison(int, int) will be called
  50.  
  51. --
  52. With best regards,
  53. Igor Boukanov (igor.boukanov@fi.uib.no).
  54.  
  55.  
  56. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  57. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  58. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  59. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  60. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  61.